package light.head.controller.site;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.config.ConfigParm;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.site.CategoryDAO;
import light.head.dao.site.CertificatesAnnexDAO;
import light.head.dao.site.CertificatesDAO;
import light.head.dao.site.CommodityDAO;
import light.head.dao.site.CommodityDetailDAO;
import light.head.dao.site.CommodityImgDAO;
import light.head.dao.site.CommodityRankDAO;
import light.head.dao.site.CommodityShowDAO;
import light.head.dao.site.OrganActivitiesDAO;
import light.head.dao.site.RecruitInfoDAO;
import light.head.dao.sys.OrganDAO;
import light.head.dao.sys.OrganDetailsDAO;
import light.head.dao.sys.OrganSynopsisAnnexDAO;
import light.head.dao.sys.OrganSynopsisDAO;
import light.head.framework.base.BaseController;
import light.head.framework.entity.PageBean;
import light.head.model.Category;
import light.head.model.Certificates;
import light.head.model.CertificatesAnnex;
import light.head.model.Commodity;
import light.head.model.CommodityDetail;
import light.head.model.CommodityImg;
import light.head.model.CommodityRank;
import light.head.model.CommodityShow;
import light.head.model.Organ;
import light.head.model.OrganActivities;
import light.head.model.OrganDetails;
import light.head.model.OrganSynopsis;
import light.head.model.OrganSynopsisAnnex;
import light.head.model.RecruitInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(Url.ORGANINDEX)
public class OrganIndexController extends BaseController{
	private OrganActivitiesDAO oaDAO;
	private OrganSynopsisDAO osDAO;
	private OrganSynopsisAnnexDAO osaDAO;
	private CategoryDAO caDAO;
	private CommodityDAO commDAO;
	private CommodityImgDAO ciDAO;
	private CommodityShowDAO csDAO;
	private CommodityRankDAO crDAO;
	private CommodityDetailDAO cdDAO;
	private CertificatesDAO cerDAO;
	private CertificatesAnnexDAO ceraDAO;
	private OrganDetailsDAO odDAO;
	private OrganDAO oDAO;
	private RecruitInfoDAO riDAO;

	
	//首页跳转
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(Integer code,HttpServletRequest request, HttpServletResponse response) throws Exception {
		//企业公告和企业动态
		List<OrganActivities> oalist1 = oaDAO.getIndexList(code, Sys.S_ZERO);
		List<OrganActivities> oalist2 = oaDAO.getIndexList(code, Sys.S_ONE);
		//公司简介
		OrganSynopsis os = osDAO.getByOc(code);
		if(os != null){
			List<OrganSynopsisAnnex> imgList = osaDAO.getImgList(code);
			List<OrganSynopsisAnnex> osaList = osaDAO.getAnnexList4(code);
			if(osaList != null && osaList.size() != 0){
				for(OrganSynopsisAnnex osa : osaList){
					if(osa.getType() == 1){
						request.setAttribute(Parm.YYLIST1, osa);
					}else if(osa.getType() == 2){
						request.setAttribute(Parm.YYLIST2, osa);
					}else if(osa.getType() == 3){
						request.setAttribute(Parm.YYLIST3, osa);
					}
				}
			}
			request.setAttribute(Parm.ORGANSYNOPSIS, os);
			request.setAttribute(Parm.YYLIST, osaList);
			request.setAttribute(Parm.OSALIST, imgList);
		}
		//产品分类
		List<Category> caList = caDAO.getIndexList(code);
		//产品展示
		List<CommodityShow> csList = csDAO.getIndexList(code);
		//产品排行
		List<CommodityRank> crList = crDAO.getIndexList(code);
		//证书荣誉
		List<Certificates> cerList = cerDAO.getIndexList(code);
		for(int i = 0; i<cerList.size(); i++){
			cerList.get(i).setList(new ArrayList<CertificatesAnnex>());
			cerList.get(i).getList().add(ceraDAO.getIndexList(cerList.get(i).getCode()));
		}
		//联系方式
		OrganDetails od = odDAO.getByOc(code);
		Organ organ = oDAO.get(code);
		//企业地图
		if(od != null){
			StringBuffer str = new StringBuffer("[");
			str.append("{\"info\":");
			str.append("[");
			str.append("\"<a href='" + ConfigParm.getContextPath() + "organindex.html?code="+ organ.getCode() + "'>" + organ.getName() + "</a>\",");
			str.append("\"电话:" + ((null == od.getPhone()) ? "暂无" : od.getPhone()) + "地址:" + ((null == od.getAddress()) ? "暂无" : od.getAddress()) + "\"");
			str.append("],");
			str.append("\"lng\":\"" + ((null == od.getLongitude()) ? "non" : od.getLongitude()) + "\",");
			str.append("\"lat\":\"" + ((null == od.getLatitude()) ? "non" : od.getLatitude()) + "\"},");
			str.deleteCharAt(str.length() - 1).append("]");
			request.setAttribute(Parm.ORGANDETAIL, str.toString());
			if(od.getLongitude() != null && od.getLatitude() != null){
				request.setAttribute(Parm.LONGITUDE, od.getLongitude());
				request.setAttribute(Parm.LATITUDE, od.getLatitude());
			}
		}
		request.setAttribute(Parm.OALIST1, oalist1);
		request.setAttribute(Parm.OALIST2, oalist2);
		request.setAttribute(Parm.CALIST, caList);
		request.setAttribute(Parm.CSLIST, csList);
		request.setAttribute(Parm.CRLIST, crList);
		request.setAttribute(Parm.CERLIST, cerList);
		request.setAttribute(Parm.ORGAN, organ);
		request.setAttribute(Parm.INFO, od);
		request.setAttribute(Parm.CODE, code);
		return new ModelAndView(Url.ORGAN_INDEX);
	}
	
	//企业首页列表页跳转
	@RequestMapping(value = Url.INITLIST)
	public ModelAndView initList(String type, Integer code,HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<CommodityRank> crList = crDAO.getIndexList(code);
		List<Category> caList = caDAO.getIndexList(code);
		List<OrganSynopsisAnnex> osaList = osaDAO.getAnnexList4(code);
		if(osaList != null && osaList.size() != 0){
			for(OrganSynopsisAnnex osa : osaList){
				if(osa.getType() == 1){
					request.setAttribute(Parm.YYLIST1, osa);
				}else if(osa.getType() == 2){
					request.setAttribute(Parm.YYLIST2, osa);
				}else if(osa.getType() == 3){
					request.setAttribute(Parm.YYLIST3, osa);
				}
			}
		}
		Organ on =oDAO.getOrgan(code);
		request.setAttribute(Parm.YYLIST, osaList);
		request.setAttribute(Parm.ON, on);
		request.setAttribute(Parm.CALIST, caList);
		request.setAttribute(Parm.CRLIST, crList);
		request.setAttribute(Parm.TYPE, type);
		request.setAttribute(Parm.OC, code);
		return new ModelAndView(Url.ORGANINIT_LIST);
	}
	/**
	 * 企业首页列表
	 */
	@RequestMapping(Url.QUERY)
	public ModelAndView queryList(String ctype ,Integer oc, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, String> map = createQueryMapByParmName(request, Parm.OC,Parm.TITLE, Parm.TIME1, Parm.TIME2,Parm.DELETED,Parm.TYPE,Parm.STATUS);
		map.put(Parm.DELETED,Sys.S_ZERO);
		PageBean pb = createPageBean(request);
		String url = null;
		if(ctype !=null){
			//公告
			if(ctype.equals("oaa")){		 
				oaDAO.findByMap(map, pb);
				url = Url.OAA_LIST;
			//动态
			}else if(ctype.equals("oat")){
				oaDAO.findByMap(map, pb);
				url = Url.OAT_LIST;
			//产品
			}else if(ctype.equals("cs")){
				csDAO.findByMap(map, pb);
				url = Url.CS_LIST;
			//证书荣誉
			}else if(ctype.equals("cer")){
				List<Certificates> cerList = cerDAO.getIndexList(oc);
				for(int i = 0; i<cerList.size(); i++){
					cerList.get(i).setList(new ArrayList<CertificatesAnnex>());
					cerList.get(i).getList().add(ceraDAO.getIndexList(cerList.get(i).getCode()));
				}
				url = Url.CER_LIST;
				request.setAttribute(Parm.CERLIST, cerList);
			//产品分类
			}else if(ctype.equals("ca")){
				List<Category> caList = caDAO.getIndexList(oc);
				request.setAttribute(Parm.CALIST, caList);
				url = Url.CA_LIST;
			//招聘
			}else if(ctype.equals("ri")){
				riDAO.findByMap(map, pb);
				url = Url.RI_LIST;
			}
		}
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(url);
	}
	
	//企业首页详情页面跳转
	@RequestMapping(value = Url.ORGANINITDETAIL)
	public ModelAndView initDetail(String type, Integer code,Integer oc, HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<CommodityRank> crList = crDAO.getIndexList(oc);
		List<Category> caList = caDAO.getIndexList(oc);
		List<OrganSynopsisAnnex> osaList = osaDAO.getAnnexList4(oc);
		if(osaList != null && osaList.size() != 0){
			for(OrganSynopsisAnnex osa : osaList){
				if(osa.getType() == 1){
					request.setAttribute(Parm.YYLIST1, osa);
				}else if(osa.getType() == 2){
					request.setAttribute(Parm.YYLIST2, osa);
				}else if(osa.getType() == 3){
					request.setAttribute(Parm.YYLIST3, osa);
				}
			}
		}
		request.setAttribute(Parm.YYLIST, osaList);
		Organ on =oDAO.getOrgan(oc);
		request.setAttribute(Parm.ON, on);
		request.setAttribute(Parm.CALIST, caList);
		request.setAttribute(Parm.CRLIST, crList);
		request.setAttribute(Parm.TYPE, type);
		request.setAttribute(Parm.OC, oc);
		request.setAttribute(Parm.CODE, code);
		return new ModelAndView(Url.ORGANINIT_DETAIL);
	}
	
	@RequestMapping(Url.DETAIL)
	public ModelAndView detail(Integer code,String ctype,Integer oc, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String url = null;
		if(ctype != null && code != null){
			//企业公告和动态
			if(ctype.equals("oaa")||ctype.equals("oat")){
				OrganActivities info = oaDAO.get(code);
				request.setAttribute(Parm.INFO, info);
				url = Url.OA_DETAIL;
			//企业简介
			}else if(ctype.equals("os")){
				OrganSynopsis info = osDAO.getByOc(oc);
				List<OrganSynopsisAnnex> osaList = osaDAO.getImgList(oc);
				request.setAttribute(Parm.INFO, info);
				request.setAttribute(Parm.OSALIST, osaList);
				url = Url.OS_DETAIL;
			//展品展示
			}else if(ctype.equals("cs")){
				Commodity info = commDAO.get(code);
				CommodityDetail cd = cdDAO.getByCode(info.getCode());
				if(cd != null){
					info.setContent(cd.getContent());
				}
				if(info.getCode() != null){
					List<CommodityImg> imgList = ciDAO.getByCode(info.getCode());
					request.setAttribute(Parm.IMGLIST, imgList);
				}
				request.setAttribute(Parm.INFO, info);
				url = Url.CS_DETAIL;
			//荣誉证书
			}else if(ctype.equals("cer")){
				Certificates info = cerDAO.get(code);
				List<CertificatesAnnex> infoImg = ceraDAO.getByCc(code);
				CertificatesAnnex cover = ceraDAO.getIndexList(info.getCode());
				request.setAttribute(Parm.INFO, info);
				request.setAttribute(Parm.IMGLIST, infoImg);
				request.setAttribute(Parm.IMG, cover);
				url = Url.CER_DETAIL;
			//产品分类
			}else if(ctype.equals("ca")){
				List<Commodity> commList = commDAO.getByCa(code);
				Category info = caDAO.get(code);
				if(info != null){
					request.setAttribute(Parm.NAME, info.getName());
				}
				request.setAttribute(Parm.CSLIST, commList);
				url = Url.CA_DETAIL;
			//招聘
			}else if(ctype.equals("ri")){
				RecruitInfo info = riDAO.get(code);
				request.setAttribute(Parm.INFO, info);
				url = Url.RI_DETAIL;
			//联系我们
			}else if(ctype.equals("cus")){
				OrganDetails od = odDAO.getByOc(oc);
				Organ organ = oDAO.get(oc);
				request.setAttribute(Parm.ORGAN, organ);
				request.setAttribute(Parm.INFO, od);
				url = Url.ORGAN_CONTACTUS;
			}
		}
		return new ModelAndView(url);
	}
	//免责申明跳转
	@RequestMapping(Url.DUTYFREE)
	public ModelAndView dutyFree(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.DUTY_FREE);
	}
	@Autowired
	public void setOaDAO(OrganActivitiesDAO oaDAO) {
		this.oaDAO = oaDAO;
	}
	@Autowired
	public void setOsDAO(OrganSynopsisDAO osDAO) {
		this.osDAO = osDAO;
	}
	@Autowired
	public void setCaDAO(CategoryDAO caDAO) {
		this.caDAO = caDAO;
	}
	@Autowired
	public void setCommDAO(CommodityDAO commDAO) {
		this.commDAO = commDAO;
	}
	@Autowired
	public void setCsDAO(CommodityShowDAO csDAO) {
		this.csDAO = csDAO;
	}
	@Autowired
	public void setCrDAO(CommodityRankDAO crDAO) {
		this.crDAO = crDAO;
	}
	@Autowired
	public void setCeraDAO(CertificatesAnnexDAO ceraDAO) {
		this.ceraDAO = ceraDAO;
	}
	@Autowired
	public void setCerDAO(CertificatesDAO cerDAO) {
		this.cerDAO = cerDAO;
	}
	@Autowired
	public void setOdDAO(OrganDetailsDAO odDAO) {
		this.odDAO = odDAO;
	}
	@Autowired
	public void setoDAO(OrganDAO oDAO) {
		this.oDAO = oDAO;
	}
	@Autowired
	public void setCiDAO(CommodityImgDAO ciDAO) {
		this.ciDAO = ciDAO;
	}
	@Autowired
	public void setCdDAO(CommodityDetailDAO cdDAO) {
		this.cdDAO = cdDAO;
	}
	@Autowired
	public void setRiDAO(RecruitInfoDAO riDAO) {
		this.riDAO = riDAO;
	}
	@Autowired
	public void setOsaDAO(OrganSynopsisAnnexDAO osaDAO) {
		this.osaDAO = osaDAO;
	}

}
